package com.guigu.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.eduservice.entity.Equipment;
import com.guigu.eduservice.entity.EquipmentImage;
import com.guigu.eduservice.entity.EquipmentVideo;
import com.guigu.eduservice.service.EquipmentImageService;
import com.guigu.eduservice.service.EquipmentService;
import com.guigu.eduservice.service.EquipmentVideoService;
import com.guigu.eduservice.vo.EquiImageDto;
import com.guigu.eduservice.vo.EquilVideoDto2;
import com.guigu.eduservice.vo.EquipmentEditDtoPC;
import com.guigu.eduservice.vo.EquipmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 设备表（存储卖家发布的二手机械信息） 前端控制器
 * </p>
 *
 * @author Xiaosong
 * @since 2025-10-05
 */
@RestController
@RequestMapping("/advertisement")
public class EquipmentController {
    // 设备表（存储卖家发布的二手机械信息）
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private EquipmentImageService equipmentImageService;
    @Autowired
    private EquipmentVideoService equipmentVideoService;
    //根据equipmentId修改设备状态
    @PutMapping("updateEquipment/{equipmentId}")
    public R updateEquipment(@PathVariable Long equipmentId){
        Equipment equipment = new Equipment();
        equipment.setEquipmentId(equipmentId);
        equipment.setStatus(2);
        equipmentService.updateById(equipment);
        return R.ok();
    }
    //查询最新上架
//    selectEquipmentInfoByTypeId
    @GetMapping("SelectEquipment")
    public R SelectEquipment(){
       List<Equipment> list = equipmentService.selectFour();
       System.out.println(list);
        return R.ok().data("list",list);
    }
    @GetMapping("SelectOrder")
    public R SelectOrder(){
        List<Equipment> list = equipmentService.list();
        System.out.println(list);
        return R.ok().data("list",list);
    }
    //根据equipmentId查询设备
    @GetMapping("/equipment/{equipmentId}")
    public R SelectEquipmentById(@PathVariable Long equipmentId){
        Equipment equipment = equipmentService.selectById(equipmentId);
        return R.ok().data("equipment",equipment);
    }
    //前台-查询所有设备-我要买
    @PostMapping("/queryEquipmentPage/{pageNo}/{pageSize}")
    public R queryEquipmentPage(@PathVariable Integer pageNo,
                                @PathVariable Integer pageSize,
                                @RequestBody EquipmentVo vo) {
        System.out.println("进入分页查询面");
        PageInfo<Equipment> list = equipmentService.queryEquipmentPage(pageNo, pageSize, vo);
        System.out.println("接收到的======="+list);
        return R.ok().data("list",list);
    }
    //前台-查询前8个品牌-我要买
    @GetMapping("/queryEquipmentBrands")
    public R queryEquipmentBrands(){
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.select("DISTINCT brand");
        equipmentQueryWrapper.last("LIMIT 8");
        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list",list);
    }
    /**
     * 前台-查询所有设备品牌名字
     * */
    @GetMapping("/selectallEquipmentBrand")
    public R selectallEquipmentBrand(){

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.select("DISTINCT brand");
        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list",list);
    }
    //前台-根据品牌查询型号-我要卖
    @GetMapping("/queryModelsByBrand")
    public R queryModelsByBrand(@RequestParam("brand") String brand) {
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT model");
        wrapper.eq("brand",brand);
        List<Equipment> models = equipmentService.list(wrapper);
        return R.ok().data("models",models);
    }
    /**
     * 前台-查询前9个省份地区
     * */
    @GetMapping("/selectfrontbaEquipmentLocation")
    public R selectfrontbaEquipmentLocation() {

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.select("DISTINCT location");
        equipmentQueryWrapper.last("LIMIT 9");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list",list);
    }
    /**
     * 前台-查询全部设备省份地区
     * */
    @GetMapping("/selectallEquipmentLocation")
    public R selectallEquipmentLocation() {

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.select("DISTINCT location");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list",list);
    }
    /**
     * 前台-我要卖上传设备信息
     * */
    @PostMapping("/saveEquipmentInfoPC")
    public  R  saveEquipmentInfoPC(@RequestBody EquipmentEditDtoPC equipmentEditDtoPC) {
            System.out.println("进入前台-我要卖上传设备信息");
            System.out.println(equipmentEditDtoPC);
        if (equipmentEditDtoPC.getEquipment()!=null){

            //拿到传递的设备对象
            Equipment equipment = equipmentEditDtoPC.getEquipment();

            //先去编辑设备信息
            Equipment equipmentDx = new Equipment();
            equipmentDx.setUserId(equipment.getUserId());
            equipmentDx.setTypeId(equipment.getTypeId());
            equipmentDx.setBrand(equipment.getBrand());
            equipmentDx.setModel(equipment.getModel());
            equipmentDx.setYear(equipment.getYear());
            equipmentDx.setHour(equipment.getHour());
            equipmentDx.setPrice(equipment.getPrice());
            equipmentDx.setVerticalHeight(equipment.getVerticalHeight());
            equipmentDx.setChassisAxle(equipment.getChassisAxle());
            equipmentDx.setOutriggerType(equipment.getOutriggerType());
            equipmentDx.setEmissionStandard(equipment.getEmissionStandard());
            equipmentDx.setLocation(equipment.getLocation());
            equipmentDx.setContactName(equipment.getContactName());
            equipmentDx.setContactPhone(equipment.getContactPhone());
            equipmentDx.setReleaseTime(LocalDateTime.now());
            equipmentDx.setCreateTime(LocalDateTime.now());
            equipmentDx.setUpdateTime(LocalDateTime.now());
            equipmentDx.setDescription(equipment.getDescription());
            //转为bloolean
            equipmentDx.setStatus(0);
            equipmentDx.setIsDetected(0);
            //添加
            equipmentService.save(equipmentDx);

            //设备图片集合
            if (equipmentEditDtoPC.getEquiImageDtoList()!=null){
                //判断封面图片
                int index = 0;
                for (EquiImageDto equiImage : equipmentEditDtoPC.getEquiImageDtoList()) {
                    //重新添加设备图片
                    EquipmentImage equipmentImage = new EquipmentImage();
                    equipmentImage.setEquipmentId(equipmentDx.getEquipmentId());
                    equipmentImage.setImageUrl(equiImage.getUrl());
                    equipmentImage.setIsCover(index == 0 ? 1 : 0); // 第一张为封面
                    equipmentImage.setCreateTime(LocalDateTime.now());
                    equipmentImage.setSort(index);
                    index++;
                    //添加设备图片
                    equipmentImageService.save(equipmentImage);
                }

                //判断设备视频
                if (equipmentEditDtoPC.getEquilVideoDto2()!=null) {
                    EquilVideoDto2 equilVideoDto2 = equipmentEditDtoPC.getEquilVideoDto2();

                    EquipmentVideo equipmentVideo = new EquipmentVideo();
                    equipmentVideo.setEquipmentId(equipmentDx.getEquipmentId());
                    equipmentVideo.setVideoUrl(equilVideoDto2.getUrl());
                    equipmentVideo.setVideoName(equilVideoDto2.getName());
                    equipmentVideo.setVideoSize(Long.valueOf(equilVideoDto2.getSize()));
                    equipmentVideo.setCreateTime(LocalDateTime.now());
                    //设置封面为第一张图片
                    equipmentVideo.setThumbnail(equipmentEditDtoPC.getEquiImageDtoList().get(0).getUrl());
                    //添加设备视频
                    equipmentVideoService.save(equipmentVideo);
                }
            }
        }

        return R.ok().code(20000).message("添加成功").data("success", true);
    }
    /**
     * 前台-根据设备类型id查询下面的品牌名称
     * */
    @GetMapping("/selectEquipmentInfoByTypeId/{id}")
    public  R  selectEquipmentInfoByTypeId(@PathVariable("id") Integer id) {

        //查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("type_id", id);
        equipmentQueryWrapper.select("DISTINCT brand");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list", list);
    }

    /**
     * 前台-点击设备品牌去查询下面的品牌型号
     * */
    @GetMapping("/selectEquipmentInfoByBrandName/{brand}")
    public  R  selectEquipmentInfoByBrandName(@PathVariable("brand") String brand) {

        //查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("brand", brand);
        equipmentQueryWrapper.select("DISTINCT model");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list", list);
    }

    /**
     * 前台-查询所有的支腿形式
     * */
    @GetMapping("/selectAllEquipmentoutriggerType")
    public  R  selectAllEquipmentoutriggerType() {

        //查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.select("DISTINCT outrigger_type");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list", list);
    }


    /**
     * 前台-查询所有的排放标准
     * */
    @GetMapping("/selectAllEquipmentemissionStandard")
    public  R  selectAllEquipmentemissionStandard() {

        //查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.select("DISTINCT emission_standard");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list", list);
    }
}
